// pages/user/user.js
import {Base} from '../../utils/base.js';
import { hex_md5 } from '../../utils/md5.js'

const app = getApp()
const base = new Base()
const updateUser = '/purchase/user/updateUser'

Page({

  /**
   * 页面的初始数据
   */
  data: {

      userInfo: null,
      isShowPopup:false,
      userName:'',
      phone: '',
      nowPassword: '',
      newPassword1: '',
      newPassword2:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let userInfo = wx.getStorageSync('userInfo')
    this.setData({
        userInfo: userInfo,
    })
    // wx.setNavigationBarTitle({
    //     title: '我的信息'
    // })
    // wx.setNavigationBarColor({
    //     frontColor: '#000000',
    //     backgroundColor: '#ffffff',
    //     animation: {
    //         duration: 400,
    //         timingFunc: 'easeIn'
    //     }
    // })
  },
  setUserName(e){
      this.setData({
          userName: e.detail.value
      })
  },
  setPhone(e) {
      this.setData({
          phone: e.detail.value
      })
  },
  formSubmit(e){
      console.log('form', e.detail.value)
    let val = e.detail.value
    let userInfo = wx.getStorageSync('userInfo')
    if (val.username.trim() == ''){
        base.showMyTip({ _this: this, title: "请输入昵称" });
    } else if (val.username.length > 6) {
        base.showMyTip({ _this: this, title: "昵称不能超过六个字符 " });
    } else if (val.phone == '' || !/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(val.phone)){
        base.showMyTip({ _this: this, title: "请输入正确的手机号" });
    } else if (val.nowpassword == '' || (hex_md5(val.nowpassword) != userInfo.password)){
        base.showMyTip({ _this: this, title: "请输入正确的当前密码" });
    } else if ((val.newpassword1 != '' || val.newpassword2 != '') && (val.newpassword1 != val.newpassword2)){
        base.showMyTip({ _this: this, title: "两次输入的新密码不一致" });
    } else if ((val.newpassword1 != '' || val.newpassword2 != '') && val.newpassword1.trim().length < 6){
        base.showMyTip({ _this: this, title: "请输入六位数以上的新密码" });        
    }else {
        let params = {
            url: updateUser,
            data:{
                "data": {
                    "id": userInfo.id,
                    "name": userInfo.name,
                    "nickName": val.username,
                    "password": val.newpassword1 == '' ? userInfo.password : hex_md5(val.newpassword1),
                    "role": userInfo.roleId,
                    "status": userInfo.status

                },
            },
            sCallback(res){
                userInfo.nickName = val.username
                if (val.newpassword1 !== ''){
                    userInfo.password = hex_md5(val.newpassword1)
                }
                wx.setStorageSync('userInfo', userInfo)
                wx.hideLoading()                        
                wx.showToast({
                    title: '修改成功',
                    icon: 'success',
                    duration: 2000,
                    success(){
                        console.log('success')
                        setTimeout(()=>{
                            if (val.newpassword1 != ''){
                                wx.removeStorageSync('userInfo')
                                // wx.setStorageSync('loginStatus',false)
                                wx.reLaunch({
                                    url: '/pages/login/login'
                                })
                                return
                            }
                            var pages = getCurrentPages();
                            var prePage = pages[pages.length - 2];
                            prePage.updateUserInfo();
                            wx.navigateBack({
                                delta: 1,
                            })
                        },2000)
                    }
                })
            },
            eCallback(e){
                base.showMyTip({ _this: this, title: e.message });        
                wx.hideLoading()
            }
        }
        wx.showModal({
            title: '提示',
            content: '确认保存吗？',
            success: function (res) {
                if (res.confirm) {
                    wx.showLoading({
                        title:'加载中'
                    })

                    base.request(params)

                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    }
  },
  formReset(e){

  },
  showToast(txt){
      wx.showToast({
          title: txt,
          image: '../../images/icon/fail.png',
          duration: 2000
      })
  },
  save(){
      console.log(this.data.userName)
  },
  cancel(){
      wx.navigateBack({
          delta: 1
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})